<template>
  <div class="changepwd-container">
    <el-row class="bread-nav">
      <ul class="bread-nav-title">
        <span>变更密码</span>
      </ul>
    </el-row>
    <el-form autoComplete="on" :model="changePwdForm" :rules="changeRules" ref="changePwdForm" label-position="left" label-width="300px" class="card-box login-form">
      <!--<h3 class="title">-->
        <!--修改密码-->
        <!--<span class="show-pwd" @click="showPwd">-->
          <!--<el-tooltip class="item" effect="light" content="显示/隐藏密码" placement="top-start">-->
            <!--<svg-icon icon-class="eye" />-->
          <!--</el-tooltip>-->
        <!--</span>-->
      <!--</h3>-->

      <el-alert class="tips" v-if="errorMsg.length>0" :title="errorMsg" type="error" show-icon :closable="false">
      </el-alert>

      <el-form-item prop="oldpassword" label="旧密码">
        <!--<span class="svg-container">-->
          <!--<svg-icon icon-class="password"></svg-icon>-->
        <!--</span>-->
        <el-input name="oldpassword" :type="pwdType" @keyup.enter.native="handleChange" v-model="changePwdForm.oldpassword"></el-input>
      </el-form-item>

      <el-form-item prop="password" label="新密码">
        <!--<span class="svg-container">-->
          <!--<svg-icon icon-class="password"></svg-icon>-->
        <!--</span>-->
        <el-input name="password" :type="pwdType" @keyup.enter.native="handleChange" v-model="changePwdForm.password"></el-input>
      </el-form-item>

      <el-form-item prop="repassword" label="确认新密码" style="border-bottom: 1px solid #b9c2cb">
        <!--<span class="svg-container">-->
          <!--<svg-icon icon-class="password"></svg-icon>-->
        <!--</span>-->
        <el-input name="repassword" :type="pwdType" @keyup.enter.native="handleChange" v-model="changePwdForm.repassword"></el-input>
      </el-form-item>

        <el-button type="primary" style="width:200px;" :loading="loading" @click.native.prevent="handleChange">
          提交
        </el-button>

    </el-form>
  </div>
</template>

<script>
  import { Message } from 'element-ui'

  export default {
    name: 'changepwd',
    data() {
      return {
        loading: false,
        pwdType: 'password',
        errorMsg: '',
        changePwdForm: {
          oldpassword: '',
          password: '',
          repassword: '',
        },
        changeRules: {
          oldpassword: [
            {
              type: 'string',
              required: true,
              trigger: 'blur',
              message: '请输入旧密码',
            },
          ],
          password: [
            {
              type: 'string',
              required: true,
              trigger: 'blur',
              message: '请输入新密码',
            },
            {
              min: 6,
              max: 10,
              trigger: 'change',
              message: '长度在6-10位',
            },
            {
              pattern: /^[a-zA-Z0-9]+$/,
              trigger: 'blur',
              message: '只能输入英文字符和数字',
            },
          ],
          repassword: [
            {
              type: 'string',
              required: true,
              trigger: 'blur',
              message: '请确认密码',
            },
            {
              min: 6,
              max: 10,
              trigger: 'change',
              message: '长度在6-10位',
            },
            {
              // 自定义验证
              validator: (rule, value, callback) => {
                if (value !== this.changePwdForm.password) {
                  callback(new Error('两次输入的密码不一致!'))
                } else {
                  callback()
                }
              },
              trigger: 'blur',
            },
          ],
        },
      }
    },

    methods: {
      showPwd() {
        if (this.pwdType === 'password') {
          this.pwdType = ''
        } else {
          this.pwdType = 'password'
        }
      },
      checkConfirm(param) {
        // 验证新密码是否一致
      },
      handleChange() {
        this.$refs.changePwdForm.validate(valid => {
          if (valid) {
            this.loading = true
            this.$store
              .dispatch('ChangePwd', { ChangePwdForm: this.changePwdForm })
              .then(response => {
                this.loading = false
                this.errorMsg = ''
                if (response === true) {
                  // 修改成功
                  Message.success('修改成功,请重新登录')
                  // 清空状态,重新登录
                  this.$store
                    .dispatch('FedLogOut')
                    .then(this.$router.push({ path: '/login' }))
                } else {
                  // 修改失败
                  this.errorMsg = Object.values(response)[0].toString() // 取第一个错误信息
                }
              })
              .catch(() => {
                this.loading = false
              })
          } else {
            console.log('validate fail')
            return false
          }
        })
      },
    },
  }
</script>

<style rel="stylesheet/scss" lang="scss">
  $bg: white;
  $dark_gray: #889aa4;
  $light_gray: #eee;
  $light_blue: #b3d8ff;
  $dark_blue: #ecf5ff;

  .changepwd-container {
    position: fixed;
    height: 100%;
    width: 100%;
    background-color: $bg;
    input:-webkit-autofill {
      -webkit-box-shadow: 0 0 0px 1000px white inset !important;
      -webkit-text-fill-color: black !important;
    }
    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: black;
      height: 47px;
    }
    .el-input {
      display: inline-block;
      height: 47px;
      width: 85%;
    }
    .tips {
      font-size: 14px;
      margin-bottom: 10px;
    }
    .svg-container {
      padding: 6px 5px 6px 15px;
      color: $dark_gray;
      vertical-align: middle;
      width: 30px;
      display: inline-block;
      &_login {
        font-size: 20px;
      }
    }
    .title {
      font-size: 26px;
      color: black;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
    .login-form {
      /*position: absolute;*/
      /*left: 0;*/
      /*right: 0;*/
      width: 100%;
      padding: 10px;
      margin: 10px auto;
    }
    .el-form-item {
      border: 1px solid #b9c2cb; // background: rgba(0, 0, 0, 0.1);
      border-bottom: 0;
      color: #454545;
      height:40px;
      line-height: 40px;
      margin: 0;
    }
    .el-form-item__content{
      height:40px;
      line-height: 40px;
      padding: 5px 10px;
    }
    .el-form-item__label{
      color: #454545;
      height:40px;
      line-height: 40px;
      text-align: right;
      background-color: #F2F2F2;
      border-bottom: 1px solid #b9c2cb;
      border-right: 1px solid #b9c2cb;
    }
    .el-input{
      border: 1px solid #b9c2cb; // background: rgba(0, 0, 0, 0.1);
      color: #454545;
      height:30px;
      line-height: 30px;
      width:200px;
      padding: 0 10px;
      display: block;
    }
    .el-input__inner{
      height:25px;
      line-height: 30px;
      padding: 0;
    }
    .el-form-item__error{
      height:30px;
      position: absolute;
      line-height: 30px;
      margin-left: 220px;
      margin-top: -40px;
    }
    .el-button{
      height:30px;
      margin-top: 10px;
      line-height: 30px;
      padding: 0;
      margin-left: 10px;
    }
    .show-pwd {
      position: absolute;
      right: 10px;
      top: 7px;
      font-size: 16px;
      color: $dark_gray;
      cursor: pointer;
      user-select: none;
    }
    .verify-code {
      width: 40%;
    }
    .show-code {
      position: absolute;
      right: 10px;
      width: 40%;
      top: 7px;
      font-size: 16px;
      color: $dark_gray;
      cursor: pointer;
      user-select: none;
    }
    .thirdparty-button {
      position: absolute;
      right: 35px;
      bottom: 28px;
    }
    .bread-nav{
      height:30px;
      font-size: 13px;
      line-height: 30px;
      background: #ECEFF2;
      border-bottom: 1px solid #d0dff2;
      padding: 0 0 0 10px;
      overflow: hidden;
    }
    .bread-nav-title{
      color:#386399;
      font-weight: bold;
      background: url("../../../../static/agent/img/tb.png") no-repeat left center;
      padding-left: 20px;
    }
  }
</style>
